﻿package smooth.core.object.localizationComponent {
	import app.core.action.RotatableScalable;
	import flash.display.Sprite;
	import smooth.core.object.flickrComponent.FlickrComponent;
	
	import flash.events.*;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.AntiAliasType;
	import flash.display.Shape;	
	
	import caurina.transitions.Tweener;	
	
	public class LocalizationComponent extends RotatableScalable {
		
		[Embed(source="Arial.ttf", fontFamily="myFont")] 
		public var myFont:Class;
		private var lat:Number;
		private var lng:Number;
		private var label:String;
		private var initialX:Number;
		private var initialY:Number;
		private var clickgrabber:Shape = new Shape();	
		public var t:TextField;
		
		public function LocalizationComponent(point:Point, lat:Number, lng:Number, label:String) {
			// Init des attributs de l'objet
			this.lat = lat;
			this.lng = lng;
			this.label = label;
			this.initialX = point.x;
			this.initialY = point.y;
			// Init des visuels du composant			
			initGraphics();
			// Init des process du composant
			initProcess();
			// Init des events du composant
			initEvents();	
		}
		
		private function initGraphics():void {
			clickgrabber.graphics.lineStyle(0,0xFFFFFF,0);
			clickgrabber.graphics.beginFill(0xFFFFFF,0);
			clickgrabber.graphics.drawRoundRect(-25,-25,50,50,1,1);
			clickgrabber.graphics.endFill();
			
			this.alpha = 1;
			this.graphics.lineStyle(5, 0x0000FF);	
			this.graphics.drawCircle(0, 0, 15);
			this.graphics.lineStyle(3, 0xFFFFFF);	
			this.graphics.moveTo (0, 25);
			this.graphics.lineTo (0, -25);
			this.graphics.moveTo (-25, 0);
			this.graphics.lineTo (25, 0);
			this.graphics.endFill();
			this.addChild(clickgrabber);
			
			var noSelection:Boolean = true;			
			var format:TextFormat= new TextFormat();
			format.font= "myFont";
			format.color= 0xFFFFFF;
			format.size= 10;
			
			t = new TextField();
			t.background = true;	
			t.backgroundColor = 0x000000;	
			t.border = true;	
			t.borderColor = 0x333333;			
			t.embedFonts = true;
			t.selectable  = true;		 
			t.text = this.label;
			t.autoSize = TextFieldAutoSize.CENTER;
			t.name = "t";
			t.x = 0;
			t.y = 35;
			t.setTextFormat(format);
			t.visible = false;
			this.addChild(t);	 
			
			this.x = this.initialX;
			this.y = this.initialY;
			
		}
		
		private function initProcess():void {
		}
		
		private function initEvents():void {
			this.addEventListener(TouchEvent.MOUSE_UP, this.doTouchUp);
			this.addEventListener(MouseEvent.MOUSE_UP, this.doMouseUp);
		}
		
		public function getLat():Number {
			return this.lat;
		}
		
		public function getLng():Number {
			return this.lng;
		}
		
		private function doMouseUp(e:MouseEvent):void {
			var point:Point = new Point(e.stageX, e.stageY);
			doProcessMouseUp(point);
		}
		
		private function doTouchUp(e:TouchEvent):void {
			var point:Point = new Point(e.stageX, e.stageY);
			doProcessMouseUp(point);
		}
  
		private function doProcessMouseUp(point:Point):void {
			var actionsToProcess:Array = new Array();
			var targetObjects:Array = this.stage.getObjectsUnderPoint(point);
			actionsToProcess.unshift({objectType:"", targetObject:null})
			for (var i:int = 0; i < targetObjects.length;++i) {
				var targetObject:Object = targetObjects[i];
				trace(targetObject);
				if (targetObject is FlickrComponent) {
					actionsToProcess.unshift({objectType:"FlickrComponent", targetObject:targetObject});
				}
			}
			
			if (actionsToProcess[0].objectType == "FlickrComponent") {
				actionsToProcess[0].targetObject.geoSearchService(this);
				Tweener.addTween(this, {alpha:0,scaleX: 0.0, scaleY: 0.0, time:0.25, transition:"easeinoutquad"});	
			}
		}
		
	}
	
}